//
//  VKStudyDaCardView.m
//  V-Learn
//
//  Created by liubo on 2019/3/1.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKMePeibanPersonView.h"

@implementation VKMePeibanPersonView


- (instancetype)initWithTitle:(NSString *)titleString withDataArray:(NSMutableArray *)dataArray withTitleArray:(NSMutableArray *)titleArray
{
    self = [super init];
    if (self) {
        self.dataArray = dataArray;
        self.titleArray = titleArray;
        self.titleString = titleString;
        [self setuplayout];
    }
    return self;
}
- (NSMutableArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = [[NSMutableArray alloc]init];
    }
    return _titleArray;
}
- (NSMutableArray *)personArray{
    if (_personArray == nil) {
        _personArray = [[NSMutableArray alloc]init];
    }
    return _personArray;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(void)setuplayout{
    UIButton *downbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    downbtn.backgroundColor = [UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:0.4];
    
    [self addSubview:downbtn];
    [downbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
//    [downbtn addTarget:self action:@selector(downbtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen cz_screenHeight], [UIScreen cz_screenWidth], 250)];
    [downbtn addSubview:downView];
    self.downView = downView;
    downView.backgroundColor = [UIColor whiteColor];
    
    [UIView animateWithDuration:0.15 animations:^{
        downView.frame = CGRectMake(0, [UIScreen cz_screenHeight] - 250, [UIScreen cz_screenWidth], 250);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor cz_colorWithRed:154 green:154 blue:154] forState:UIControlStateNormal];
    [downView addSubview:cancelBtn];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(downView).offset(15);
        make.top.mas_equalTo(0);
        make.width.equalTo(@50);
        make.height.equalTo(@45);

    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = self.titleString;
    [downView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(downView);
        make.centerY.mas_equalTo(cancelBtn);
    }];
    
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor cz_colorWithRed:56 green:56 blue:56] forState:UIControlStateNormal];
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [downView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.width.equalTo(@50);
        make.height.equalTo(@45);

    }];
    UIView *lineView = [[UIView alloc]init];
    [downView addSubview:lineView];
    lineView.backgroundColor = [UIColor cz_colorWithRed:229 green:229 blue:229];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.right.equalTo(@0);
        make.top.mas_equalTo(commitBtn.mas_bottom);
    }];
     //添加
    
//    NSArray *titleArray = self.titleArray;
    
    CGFloat x = 15;
    CGFloat y = 80;
    
    for (int i = 1; i < self.titleArray.count + 1; i++) {
        UILabel *label = [self labelWithTitle:self.titleArray[i - 1]];
        CGFloat width = label.frame.size.width + 20;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor clearColor];
        // label.tag = i;
        if (x + width + 15 > [UIScreen cz_screenWidth]) {
            y += 40;//换行
            x = 15; //15位置开始
        }
        
        label.frame = CGRectMake(x, y, width, 30);
        [downView addSubview:label];
        x += width + 10;//宽度+间隙
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [label addSubview:button];
        [button setTitle:self.titleArray[i - 1] forState:UIControlStateNormal];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@0);
            make.width.equalTo(label.mas_width);
            make.height.equalTo(label.mas_height);
        }];
        button.tag = i;
        if ([self.dataArray containsObject:[NSString stringWithFormat:@"%d",i]]) {
            button.selected = 1;
            [self.personArray addObject:button.titleLabel.text];
        }
        
//        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor cz_colorWithRed:242 green:242 blue:242] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor cz_colorWithRed:102 green:102 blue:102] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
//        int width = ([UIScreen cz_screenWidth] - 90) / 4;
//
//        if (i > 4) {
//            button.frame = CGRectMake(15 + (i - 5) * (width + 20) , 80 + (36 + 20), width, 36);
//        }else{
//            button.frame = CGRectMake(15 + (i - 1) * (width + 20) , 80, width, 36);
//        }
        
        if (button.selected) {
            
            button.backgroundColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
        }else{
            button.backgroundColor = [UIColor cz_colorWithRed:242 green:242 blue:242];
        }
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    }
}

- (void)buttonClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        sender.backgroundColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
        [self.dataArray addObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
        [self.personArray addObject:sender.titleLabel.text];
    }else{
        
        [self.dataArray removeObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
        [self.personArray removeObject:sender.titleLabel.text];
        sender.backgroundColor = [UIColor cz_colorWithRed:242 green:242 blue:242];
        
    }
}
// 确定
- (void)commitBtnClick{
//    NSLog(@"array:%@",self.dataArray);
    if (_personViewBlock) {
        self.personViewBlock(self.dataArray,self.personArray);
    }
}
// 取消
- (void)cancelBtnClick{
    [UIView animateWithDuration:0.15 animations:^{
        self.downView.frame = CGRectMake(0, [UIScreen cz_screenHeight], [UIScreen cz_screenWidth], 250);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
//        self.hidden = YES;

    }];
}


// 取消
- (void)cancelDownView{
    [UIView animateWithDuration:0.15 animations:^{
        self.downView.frame = CGRectMake(0, [UIScreen cz_screenHeight], [UIScreen cz_screenWidth], 250);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (UILabel *)labelWithTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    [label sizeToFit];
    label.layer.cornerRadius = 6;
    label.layer.borderWidth = 1;
    label.layer.borderColor = [UIColor cz_colorWithRed:227 green:227 blue:227].CGColor;
    label.layer.masksToBounds = YES;
    label.userInteractionEnabled = YES;
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}
@end
